﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class service_yuyue : System.Web.UI.Page
{
    Web_TrainBLL bll = new Web_TrainBLL();
    public static string[] str = { };//预约码数据
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                string id = Request.QueryString["id"].ToString();
                hidid.Value = id;
                ShowPlaceInfo(int.Parse(id));
            }
        }
    }
    /// <summary>
    /// 显示会场相关信息
    /// </summary>
    /// <param name="id"></param>
    public void ShowPlaceInfo(int id)
    {
        DataSet ds = bll.GetPlaceInfoById(id);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                lblplacename.Text = ds.Tables[0].Rows[0]["place_name"].ToString();
                lblteachername.Text = ds.Tables[0].Rows[0]["teachername"].ToString();
                lblteachername1.Text = ds.Tables[0].Rows[0]["teachername"].ToString();
                lbltrainintro.Text = ds.Tables[0].Rows[0]["train_intro"].ToString();
                lbltraintime.Text = ds.Tables[0].Rows[0]["train_time"].ToString();
                lblteacherintro.Text = ds.Tables[0].Rows[0]["teacherintro"].ToString();
                teachimg.ImageUrl = "http://i.ymcps.com/BugView/" + ds.Tables[0].Rows[0]["teacherimg"].ToString();
                hidarea.Value = ds.Tables[0].Rows[0]["area"].ToString();

                int allnum = int.Parse(ds.Tables[0].Rows[0]["allnum"].ToString());
                int num = int.Parse(ds.Tables[0].Rows[0]["num"].ToString());

                lblnum.Text = (allnum - num).ToString();
                notrain.Visible = false;
            }
            else
            {
                traininfo.Visible = false;
                notrain.Visible = true;
            }
        }
        else
        {
            traininfo.Visible = false;
            notrain.Visible = true;
        }
    }
    /// <summary>
    /// 我要参加按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_canjia_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            string area = hidarea.Value;
            string username = txt_username.Text;
            string mobile = txt_mobile.Text;
            int id = int.Parse(hidid.Value);
            emoneybussiness.emoneybusiness sv = new emoneybussiness.emoneybusiness();
            DataSet ds1 = sv.GetResource(username);
            if (ds1 != null)
            {
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    string coname = ds1.Tables[0].Rows[0]["CoName"].ToString();
                    if (coname != "" || coname != null)
                    {
                        if (!coname.Contains(area.Trim()))  //注册地和预约地区一致有预约资格
                        {
                            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('您的用户注册地区和培训所属地区不一致，请核对后再进行预约！(可致电4006708688修改注册地)')", true);
                            return;
                        }
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('您的用户注册地区信息还不完善，请完善后再进行预约！')", true);
                        return;
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "aa", "alert('没有您的用户注册地区信息，请注册后再进行预约！')", true);
                    return;
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('服务器繁忙，请稍后再试！')", true);
                return;
            }
            lock (str)
            {
                int syrs = 0;//预约总人数（各个会场不同）
                int yyrs = 0;//已预约人数
                string placenum = "";
                DataSet ds = bll.GetPlaceInfoById(id);  //获取预约总人数和已预约人数
                placenum = ds.Tables[0].Rows[0]["place_num"].ToString();
                lblarea.Text = area;
                lblplacename1.Text = ds.Tables[0].Rows[0]["place_name"].ToString();
                lblteachername2.Text = ds.Tables[0].Rows[0]["teachername"].ToString();
                lbltrainaddr.Text = ds.Tables[0].Rows[0]["train_addr"].ToString();
                lbltraintime1.Text = ds.Tables[0].Rows[0]["train_time"].ToString();
                lblremark.Text = ds.Tables[0].Rows[0]["remark"].ToString();
                syrs = int.Parse(ds.Tables[0].Rows[0]["allnum"].ToString());
                yyrs = int.Parse(ds.Tables[0].Rows[0]["num"].ToString());

                string reservenum = "";//预约码
                int result = 0; //提交结果
                str = Easy(syrs); //生成预约码
                if (yyrs > syrs || yyrs == syrs)
                {
                    result = 11;//预约名额已满提示
                }
                else
                {
                    reservenum = str[yyrs].ToString();
                    lblreservenum.Text = reservenum;
                    //插入用户预约成功信息
                    result = int.Parse(bll.InsertUserInfo(username, area, placenum, reservenum, 0));

                }
                //预约成功就发送短信(手机号不为空)
                
                if (result > 0)
                {
                    if (result == 11)
                    {
                        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('预约名额已满,敬请关注下场培训！')", true);
                        divok.Visible = false;
                    }
                    else if (result == 22)
                    {
                        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('您已经预约过此培训，请勿重复预约！')", true);
                        divok.Visible = false;
                    }
                    else
                    {
                        string time = lbltraintime1.Text;
                        string address = lbltrainaddr.Text;
                        if (mobile != "")
                        {
                            //插入用户手机信息 
                            sv.AddActiveIsExist(mobile, 0, 0, "2011_08_26_hcyy", "客服专区全国会场预约", "", "", username, "", area, "", DateTime.Now, "");
                            //sv.ActMobileInsert(mobile, username, area, 0, 0, "2011_08_26_hcyy", "客服专区全国会场预约", 0, "");
                            //发送短信
                            FeeDBWS.FeeDBWS service = new FeeDBWS.FeeDBWS();
                            service.GetValidateCodeForMobile(mobile, "您的预约已成功，预约号为" + reservenum + "，" + time + "，地址：" + address + "，敬请准时到场参加！");
                        }

                        divok.Visible = true;
                    }
                }
            }
        }
        catch (Exception exp)
        {
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('"+exp.Message+"')", true);
        }
    }

    /// <summary>
    /// 自动生成预约码
    /// </summary>
    /// <param name="count">剩余名额</param>
    /// <returns></returns>
    private static string[] Easy(int count)
    {
        int n = (count + 100 - 1) / 100;
        string[] people = new string[count];
        int[] letter = new int[n];
        for (int i = 0; i < letter.Length; i++)
        {
            letter[i] = 65 + i;
        }

        for (int i = 0; i < (count + n - 1) / n; i++)
        {
            string str = i.ToString().Length == 1 ? "0" + i : "" + i;
            for (int j = 0; j < n; j++)
            {
                if (i * n + j < count)
                {
                    people[i * n + j] = (char)letter[j] + str;
                }
            }
        }
        return people;
    }
}